/*
 * Teknei 2014 - All rights reserved
 */
package com.teknei.acabus.core.persistence.dao.impl;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.teknei.acabus.PersistenceException;
import com.teknei.acabus.core.entities.NavigationChild;
import com.teknei.acabus.core.entities.NavigationChildRole;
import com.teknei.acabus.core.entities.NavigationParent;
import com.teknei.acabus.core.entities.NavigationParentRole;
import com.teknei.acabus.core.persistence.dao.NavigationDAO;

/**
 * Session Bean implementation class NavigationDAOBean
 * 
 * @author Jorge Amaro
 * @version 1.0
 * @since 1.0
 */
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public class NavigationDAOBean implements NavigationDAO {

	private final static Logger LOG = LoggerFactory
			.getLogger(NavigationDAOBean.class);

	@PersistenceContext
	EntityManager em;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.teknei.acabus.core.persistence.dao.NavigationDAO#createNavigationParent
	 * (com.teknei.acabus.core.entities.NavigationParent)
	 */
	@Override
	public NavigationParent createNavigationParent(NavigationParent t)
			throws PersistenceException {
		try {
			this.em.persist(t);
			this.em.flush();
			this.em.refresh(t);
			return t;
		} catch (Exception e) {
			throw new PersistenceException("Error in createNavigationParent:"
					+ e.getMessage(), e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.teknei.acabus.core.persistence.dao.NavigationDAO#createNavigationChild
	 * (com.teknei.acabus.core.entities.NavigationChild)
	 */
	@Override
	public NavigationChild createNavigationChild(NavigationChild t)
			throws PersistenceException {
		try {
			this.em.persist(t);
			this.em.flush();
			this.em.refresh(t);
			return t;
		} catch (Exception e) {
			throw new PersistenceException("Error in createNavigationChild:"
					+ e.getMessage(), e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.teknei.acabus.core.persistence.dao.NavigationDAO#deleteNavigationChild
	 * (com.teknei.acabus.core.entities.NavigationChild)
	 */
	@Override
	public void deleteNavigationChild(NavigationChild t)
			throws PersistenceException {
		try {
			t = em.merge(t);
			this.em.remove(t);
			em.flush();
		} catch (Exception e) {
			throw new PersistenceException("Error in deleteNavigationChild:"
					+ e.getMessage(), e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.teknei.acabus.core.persistence.dao.NavigationDAO#deleteNavigationParent
	 * (com.teknei.acabus.core.entities.NavigationParent)
	 */
	@Override
	public void deleteNavigationParent(NavigationParent t)
			throws PersistenceException {
		try {
			t = em.merge(t);
			this.em.remove(t);
			em.flush();
		} catch (Exception e) {
			throw new PersistenceException("Error in deleteNavigationParent:"
					+ e.getMessage(), e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.teknei.acabus.core.persistence.dao.NavigationDAO#updateNavigationParent
	 * (com.teknei.acabus.core.entities.NavigationParent)
	 */
	@Override
	public NavigationParent updateNavigationParent(NavigationParent t)
			throws PersistenceException {
		try {
			return this.em.merge(t);
		} catch (Exception e) {
			throw new PersistenceException("Error in updateNavigationParent:"
					+ e.getMessage(), e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.teknei.acabus.core.persistence.dao.NavigationDAO#
	 * updateNavigationParentWithRoles
	 * (com.teknei.acabus.core.entities.NavigationParent)
	 */
	@Override
	public NavigationParent updateNavigationParentWithRoles(NavigationParent t)
			throws PersistenceException {
		try {
			NavigationParent source = em.find(NavigationParent.class,
					t.getIdParent());
			List<NavigationParentRole> listSource = source
					.getNavigationParentRoles();
			source.setNavigationParentRoles(null);
			em.merge(source);
			for (NavigationParentRole npr : listSource) {
				em.remove(npr);
			}
			source.setNavigationParentRoles(t.getNavigationParentRoles());
			for (NavigationParentRole npr : t.getNavigationParentRoles()) {
				em.persist(npr);
			}
			return em.merge(source);
		} catch (Exception e) {
			throw new PersistenceException(
					"Error in updateNavigationParentWithRoles:"
							+ e.getMessage(), e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.teknei.acabus.core.persistence.dao.NavigationDAO#updateNavigationChild
	 * (com.teknei.acabus.core.entities.NavigationChild)
	 */
	@Override
	public NavigationChild updateNavigationChild(NavigationChild t)
			throws PersistenceException {
		try {
			return this.em.merge(t);
		} catch (Exception e) {
			throw new PersistenceException("Error in updateNavigationChild:"
					+ e.getMessage(), e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.teknei.acabus.core.persistence.dao.NavigationDAO#
	 * updateNavigationChildWithRoles
	 * (com.teknei.acabus.core.entities.NavigationChild)
	 */
	@Override
	public NavigationChild updateNavigationChildWithRoles(NavigationChild t)
			throws PersistenceException {
		try {
			NavigationChild source = em.find(NavigationChild.class,
					t.getIdChild());
			List<NavigationChildRole> listSource = source
					.getNavigationChildRoles();
			source.setNavigationChildRoles(null);
			em.merge(source);
			for (NavigationChildRole npr : listSource) {
				em.remove(npr);
			}
			source.setNavigationChildRoles(t.getNavigationChildRoles());
			for (NavigationChildRole npr : t.getNavigationChildRoles()) {
				em.persist(npr);
			}
			return em.merge(source);
		} catch (Exception e) {
			throw new PersistenceException(
					"Error in updateNavigationChildWithRoles:" + e.getMessage(),
					e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.teknei.acabus.core.persistence.dao.NavigationDAO#findAllNavParent()
	 */
	@Override
	public List<NavigationParent> findAllNavParent()
			throws PersistenceException {
		try {
			TypedQuery<NavigationParent> query = em.createNamedQuery(
					"NavigationParent.findAll", NavigationParent.class);
			List<NavigationParent> list = query.getResultList();
			return list;
		} catch (Exception e) {
			throw new PersistenceException("Error in findAllNavParent:"
					+ e.getMessage(), e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.teknei.acabus.core.persistence.dao.NavigationDAO#findAllNavChild()
	 */
	@Override
	public List<NavigationChild> findAllNavChild() throws PersistenceException {
		try {
			TypedQuery<NavigationChild> query = em.createNamedQuery(
					"NavigationChild.findAll", NavigationChild.class);
			List<NavigationChild> list = query.getResultList();
			return list;
		} catch (Exception e) {
			throw new PersistenceException("Error in findAllNavChild:"
					+ e.getMessage(), e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.teknei.acabus.core.persistence.dao.NavigationDAO#findNavChildRelated
	 * (int)
	 */
	@Override
	public List<NavigationChild> findNavChildRelated(int idParent)
			throws PersistenceException {
		try {
			TypedQuery<NavigationChild> query = em.createNamedQuery(
					"NavigationChild.findRelated", NavigationChild.class);
			query.setParameter("idParent", idParent);
			List<NavigationChild> list = query.getResultList();
			return list;
		} catch (Exception e) {
			throw new PersistenceException("Error in findNavChildRelated:"
					+ e.getMessage(), e);
		}

	}

}
